import GameView from "../../../../quick/core/ui/GameView";
import { inject } from "../../../../quick/defines/Decorators";
import { Macro } from "../../../../quick/defines/Macros";
import MasaicHandler from "../handlers/MasaicHandler";
import ShaderWaterSpread from "../handlers/ShaderWaterSpread";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ShaderView extends GameView {

    static getPrefabUrl(){
        return "prefabs/ShaderView";
    }

    private preShow : cc.Node = null;
    @inject("cutSprite/show/icon3",cc.Sprite,"content")
    private sprite : cc.Sprite = null;
    @inject("darkSprite/show/test",cc.Sprite,"content")
    private darkSprite : cc.Sprite = null;
    @inject("content",cc.Node)
    private content : cc.Node = null!;
    onLoad(){
        super.onLoad();

        this.onN(cc.find("goback",this.node),cc.Node.EventType.TOUCH_END,()=>{
            this.enterBundle(Macro.BUNDLE_HALL);
        });

        cc.find("waterSpread/sprite",this.content).addComponent(ShaderWaterSpread);

        cc.find("mosaic",this.content).addComponent(MasaicHandler);

        let container = cc.find("op",this.node).getComponent(cc.ToggleContainer);
        let event = new cc.Component.EventHandler()
        event.component = "ShaderView";
        event.target = this.node;
        event.handler = "onSelected";
        container.checkEvents.push(event)

        let items = container.toggleItems;
        for ( let i = 0 ; i < items.length ; i++ ){
            if ( items[i].isChecked ){
                this.onSelected(items[i]);
                break;
            }
        }

        let round = cc.find("cutSprite/round",this.content);
        let feather = cc.find("cutSprite/feather",this.content);
        this.onN(round,"slide",this.onChangeRound);
        this.onN(feather,"slide",this.onChangeFeather);

        this.onChangeRound(round.getComponent(cc.Slider));
        this.onChangeFeather(feather.getComponent(cc.Slider));

        let rate = cc.find("darkSprite/rate",this.content);
        this.onN(rate,"slide",this.onChangeDarkRate);
        this.onChangeDarkRate(rate.getComponent(cc.Slider))
    }

    private onSelected( target : cc.Toggle ){
        if ( this.preShow ){
            this.preShow.active = false;
        }
        let node = cc.find(target.node.name,this.content);
        if ( cc.isValid(node) ){
            node.active = true;
            this.preShow = node;
        }
    }

    private onChangeRound( target : cc.Slider ){
        if ( this.sprite ){
            let material = this.sprite.getMaterial(0);
            material.setProperty("round",target.progress);
        }
        
    }

    private onChangeFeather( target : cc.Slider ){
        if ( this.sprite ){
            let material = this.sprite.getMaterial(0);
            material.setProperty("feather",target.progress);
        }
        
    }

    private onChangeDarkRate( target : cc.Slider ){
        if (this.darkSprite ){
            let material = this.darkSprite.getMaterial(0);
            material.setProperty("rate",target.progress);
        }
    }
}
